package com.trade.module.base.entity;


import com.trade.common.utils.Constants;
import com.trade.common.utils.IdGenerator;

import javax.persistence.*;
import java.util.Date;

/**
 * Class Description:基本实体类
 *
 * @Version v1.0 2017/4/21 20:20
 * @JDK version 1.8.0_51
 * @Author zfb
 */
@SuppressWarnings(value = "all")
@MappedSuperclass
public class BaseEntity {

    /** Field description：主键标识 */
    @Id
    @Column(
            name       = "id",
            unique     = true,
            nullable   = false,
            insertable = false,
            updatable  = false,
            length     = 64
    )
    private String id;



    /** Field description：数据状态（1：正常；2：删除；0：状态无效） */
    @Column(
            name = "data_flag",
            nullable = false,
            length = 5
    )
    private String dataFlag;

    /** Field description ：描述 */
    @Column(
            name   = "desc",
            length = 128
    )
    private String description;

    /** Field description ：创建人ID */
    @Column(
            name   = "creater_id",
            length = 64
    )
    private String createrId;

    /** Field description ：创建时间 */
    @Column(
            name     = "create_time",
            nullable = false,
            columnDefinition="Timestamp"
    )
    private Date createTime;

    /** Field description ：修改人ID */
    @Column(
            name   = "modify_id",
            length = 64
    )
    private String modifyId;

    /** Field description： 修改时间 */
    @Column(
            name     = "modify_time",
            nullable = false,
            columnDefinition="Timestamp"
    )
    private Date modifyTime;

    public BaseEntity() {}

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDataFlag() {
        return dataFlag;
    }

    public void setDataFlag(String dataFlag) {
        this.dataFlag = dataFlag;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getCreaterId() {
        return createrId;
    }

    public void setCreaterId(String createrId) {
        this.createrId = createrId;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getModifyId() {
        return modifyId;
    }

    public void setModifyId(String modifyId) {
        this.modifyId = modifyId;
    }

    public Date getModifyTime() {
        return modifyTime;
    }

    public void setModifyTime(Date modifyTime) {
        this.modifyTime = modifyTime;
    }

    /**
     * Method description：数据插入前的操作
     * Version:V1.0
     * Author:Shaok Lei
     * Date:2016.02.26 at 05:37:57 CST
     */
    @PrePersist
    public void setInsertBefore() {

        // 插入数据前设置创建时间、修改时间和数据状态
        this.setId(IdGenerator.createNewId());
        Date dateTime = new Date();
        this.setCreateTime(dateTime);
        this.setModifyTime(dateTime);
        this.setDataFlag(Constants.DATA_FLAG_OK);
    }

    @PreUpdate
    public void setUpdateBefore() {
        this.modifyTime = new Date();
    }
}
